<?php
/**
 *
 */
define('CURSCRIPT', 'search');
require './source/class/class_core.php';
$mainframe = & cmsCore::instance();
define('CURMODULE', 'index');
$modcachelist = array('register' => array('modreasons'));
$cachelist = $slist = array();
if (isset($modcachelist[CURMODULE])) {
	$cachelist = $modcachelist[CURMODULE];
}
$mainframe->cachelist = $cachelist;
$mainframe->init();
$navtitle = lang('core', 'Search');
define('NOROBOT', TRUE);
if (!$_G['setting']['search']['status']) {
	showmessage('Search article has been closed, please return!');
}
if ($_G['adminid'] != 1 && !($_G['group']['allowsearch'] & 1)) {
	showmessage('group_nopermission', NULL, array('grouptitle' => $_G['group']['grouptitle']), array('login' => 1));
}
$_G['setting']['search']['searchctrl'] = intval($_G['setting']['search']['searchctrl']);
$srchmod = 1;
$cachelife_time = 300;  // Life span for cache of searching in specified range of time
$cachelife_text = 3600;  // Life span for cache of text searching
$srchtype = empty($_G['gp_srchtype']) ? '' : trim($_G['gp_srchtype']);
$searchid = isset($_G['gp_searchid']) ? intval($_G['gp_searchid']) : 0;
$srchtxt = $_G['gp_srchtxt'];
$keyword = isset($srchtxt) ? htmlspecialchars(trim($srchtxt)) : '';
if (!submitcheck('searchsubmit', 1)) {
	include template('search/portal');
} else {
	$orderby = in_array($_G['gp_orderby'], array('aid')) ? $_G['gp_orderby'] : 'aid';
	$ascdesc = isset($_G['gp_ascdesc']) && $_G['gp_ascdesc'] == 'asc' ? 'asc' : 'desc';
	if (!empty($searchid)) {
		$page = max(1, intval($_G['gp_page']));
		$start_limit = ($page - 1) * $_G['tpp'];
		$index = DB::fetch_first("SELECT searchstring, keywords, num, ids FROM #__common_searchindex WHERE searchid='$searchid' AND srchmod='$srchmod'");
		if (!$index) {
			showmessage('search_id_invalid');
		}
		$keyword = htmlspecialchars($index['keywords']);
		$keyword = $keyword != '' ? str_replace('+', ' ', $keyword) : '';
		$index['keywords'] = rawurlencode($index['keywords']);
		$aids = $index['ids'];
		$multipage = multi($index['num'], 10, $page, "index.php?option=misc&task=search&searchid=$searchid&orderby=$orderby&ascdesc=$ascdesc&searchsubmit=yes");
		include template('search/portal');
	} else {
		$searchstring = 'portal|title|' . addslashes($srchtxt);
		$searchindex = array('id' => 0, 'dateline' => '0');
		$query = DB::query("SELECT searchid, dateline,
			('" . $_G['setting']['search']['searchctrl'] . "'<>'0' AND " . (empty($_G['uid']) ? "useip='$_G[clientip]'" : "uid='$_G[uid]'") . " AND $_G[timestamp]-dateline<'" . $_G['setting']['search']['searchctrl'] . "') AS flood,
			(searchstring='$searchstring' AND expiration>'$_G[timestamp]') AS indexvalid
			FROM #__common_searchindex
			WHERE srchmod='$srchmod' AND ('" . $_G['setting']['search']['searchctrl'] . "'<>'0' AND " . (empty($_G['uid']) ? "useip='$_G[clientip]'" : "uid='$_G[uid]'") . " AND $_G[timestamp]-dateline<" . $_G['setting']['search']['searchctrl'] . ") OR (searchstring='$searchstring' AND expiration>'$_G[timestamp]')
			ORDER BY flood");
		while ($index = DB::fetch($query)) {
			if ($index['indexvalid'] && $index['dateline'] > $searchindex['dateline']) {
				$searchindex = array('id' => $index['searchid'], 'dateline' => $index['dateline']);
				break;
			} elseif ($_G['adminid'] != '1' && $index['flood']) {
				showmessage('search_ctrl', 'index.php?option=misc&task=search', array('searchctrl' => $_G['setting']['search']['searchctrl']));
			}
		}
		if ($searchindex['id']) {
			$searchid = $searchindex['id'];
		} else {
			if (!$srchtxt) {
				dheader('Location: index.php?option=misc&task=search');
			}
			if ($_G['adminid'] != '1' && $_G['setting']['search']['maxspm']) {
				if ((DB::result_first("SELECT COUNT(*) FROM #__common_searchindex WHERE srchmod='$srchmod' AND dateline>'$_G[timestamp]'-60")) >= $_G['setting']['search']['maxspm']) {
					showmessage('search_toomany', 'index.php?option=misc&task=search', array('maxspm' => $_G['setting']['search']['maxspm']));
				}
			}
			$num = $ids = 0;
			$_G['setting']['search']['maxsearchresults'] = $_G['setting']['search']['maxsearchresults'] ? intval($_G['setting']['search']['maxsearchresults']) : 500;
			$srchtxtsql = addcslashes($srchtxt, '%_');
			$query = DB::query("SELECT aid FROM #__portal_article_title WHERE title LIKE '%$srchtxtsql%' ORDER BY aid DESC LIMIT " . $_G['setting']['search']['maxsearchresults']);
			while ($article = DB::fetch($query)) {
				$ids .= ',' . $article['aid'];
				$num++;
			}
			DB::free_result($query);
			$keywords = str_replace('%', '+', $srchtxt);
			$expiration = TIMESTAMP + $cachelife_text;
			DB::query("INSERT INTO #__common_searchindex (srchmod, keywords, searchstring, useip, uid, dateline, expiration, num, ids)
					VALUES ('$srchmod', '$keywords', '$searchstring', '$_G[clientip]', '$_G[uid]', '$_G[timestamp]', '$expiration', '$num', '$ids')");
			$searchid = DB::insert_id();
		}
		dheader("location: index.php?option=misc&task=search&searchid=$searchid&searchsubmit=yes");
	}
}
function bat_highlight($message, $words, $color = '#ff0000') {
	if (!empty($words)) {
		$highlightarray = explode('+', $words);
		$sppos = strrpos($message, chr(0) . chr(0) . chr(0));
		if ($sppos !== FALSE) {
			$specialextra = substr($message, $sppos + 3);
			$message = substr($message, 0, $sppos);
		}
		$message = preg_replace(array("/(^|>)([^<]+)(?=<|$)/sUe", "/<highlight>(.*)<\/highlight>/siU"), array("highlight('\\2', \$highlightarray, '\\1')", "<strong><font color=\"$color\">\\1</font></strong>"), $message);
		if ($sppos !== FALSE) {
			$message = $message . chr(0) . chr(0) . chr(0) . $specialextra;
		}
	}
	return $message;
}
